package com.travel.group.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.travel.group.entity.Comment;
import com.travel.group.entity.Group;
import com.travel.group.entity.GroupWxuser;
import com.travel.group.service.CommentService;
import com.travel.group.service.GroupService;
import com.travel.group.service.GroupWxuserService;
import com.travel.group.utils.WeChatResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@Controller
public class GroupWxuserController {

    @Autowired
    private GroupWxuserService groupWxuserService;

    @Autowired
    private GroupService groupService;

    @Resource
    private CommentService commentService;

    @RequestMapping("/wx/signGroup")
    @ResponseBody
    public Object signGroup(@RequestBody GroupWxuser groupWxuser){
        QueryWrapper<GroupWxuser> wrapper = new QueryWrapper<>();
        wrapper.eq("group_id", groupWxuser.getGroupId()).eq("wxuser_id", groupWxuser.getWxuserId());
        List<GroupWxuser> list = groupWxuserService.list(wrapper);
        if (list.size() > 0)
            return WeChatResponseUtil.fail();
        if (groupWxuserService.save(groupWxuser))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }


    @RequestMapping("/wx/getMyGroupInfo")
    @ResponseBody
    public Object getMyGroupInfo(HttpServletRequest request){
        String wxUserId = request.getParameter("userId");
        QueryWrapper<GroupWxuser> wrapper = new QueryWrapper<>();
        wrapper.eq("wxuser_id",wxUserId);
        List<GroupWxuser> groupWxuserList = groupWxuserService.list(wrapper);
        List<Integer> groupIds = new ArrayList<>();
        for (GroupWxuser groupWxuser: groupWxuserList){
            groupIds.add(groupWxuser.getGroupId());
        }
        if (groupIds.size() > 0){
            List<Group> groupList = groupService.getByGroupIds(groupIds);
            return WeChatResponseUtil.ok(groupList);
        } else {
            return WeChatResponseUtil.ok();
        }
    }

    @RequestMapping("/wx/getOneGroup")
    @ResponseBody
    public Object getOneGroup(HttpServletRequest request){
        String wxUserId = request.getParameter("id");

        QueryWrapper<Comment> wrapper = new QueryWrapper<>();
        wrapper.eq("gid",Integer.parseInt(wxUserId));
        List<Comment> groupWxuserList = commentService.list(wrapper);

        if (groupWxuserList.size() > 0){
            return WeChatResponseUtil.ok(groupWxuserList);
        } else {
            return WeChatResponseUtil.ok();
        }
    }

    @RequestMapping("/wx/outGroup")
    @ResponseBody
    public Object outGroup(HttpServletRequest request){
        String id = request.getParameter("id");
        String userId = request.getParameter("userId");
        QueryWrapper<GroupWxuser> wrapper = new QueryWrapper<>();
        wrapper.eq("wxuser_id",userId).eq("group_id",id);
        if (groupWxuserService.remove(wrapper))
            return WeChatResponseUtil.ok();
        return WeChatResponseUtil.fail();
    }

}
